<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 9.11.&nbsp; FreeBSD Implementation</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch09lev1sec10.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch09lev1sec12.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="ch09lev1sec11"></a>
<h3 class="docSection1Title">9.11. FreeBSD Implementation</h3>
<p class="docText">Having talked about the various attributes of a process, process group, session, and controlling terminal, it's worth looking at how all this can be implemented. We'll look briefly at the implementation used by FreeBSD. Some details of the SVR4 implementation of these features can be found in Williams [<a class="docLink" href="bib01.html#biblio01_068">1989</a>]. <a class="docLink" href="#ch09fig12">Figure 9.12</a> shows the various data structures used by FreeBSD.</P>
<a name="ch09fig12"></a><P><center>
<h5 class="docFigureTitle">Figure 9.12. FreeBSD implementation of sessions and process groups</H5>
<p class="docText"><div class="v1"><a target="_self" href="images/0201433079/graphics/09fig12_alt.gif;423615">[View full size image]</a></div><img border="0" alt="" width="500" height="432" SRC="images/0201433079/graphics/09fig12.gif;423615"></P>
</center></P><br>
<p class="docText"><a name="idd1e66310"></a><a name="idd1e66315"></a><a name="idd1e66318"></a><a name="idd1e66323"></a><a name="idd1e66328"></a><a name="idd1e66333"></a><a name="idd1e66338"></a><a name="idd1e66343"></a><a name="idd1e66348"></a><a name="idd1e66353"></a><a name="idd1e66358"></a><a name="idd1e66361"></a><a name="idd1e66366"></a><a name="idd1e66371"></a><a name="idd1e66376"></a><a name="idd1e66381"></a><a name="idd1e66386"></a><a name="idd1e66391"></a>Let's look at all the fields that we've labeled, starting with the <tt>session</tt> structure. One of these structures is allocated for each session (e.g., each time <tt>setsid</tt> is called).</P>
<UL><LI><p class="docList"><tt>s_count</tt> is the number of process groups in the session. When this counter is decremented to 0, the structure can be freed.</p></LI><li><p class="docList"><tt>s_leader</tt> is a pointer to the <tt>proc</tt> structure of the session leader.</P></LI><LI><p class="docList"><tt>s_ttyvp</tt> is a pointer to the <tt>vnode</tt> structure of the controlling terminal.</p></LI><LI><p class="docList"><tt>s_ttyp</tt> is a pointer to the <tt>tty</tt> structure of the controlling terminal.</p></LI><LI><p class="docList"><tt>s_sid</tt> is the session ID. Recall that the concept of a session ID is not part of the Single UNIX Specification.</p></li></ul>
<p class="docText">When <tt>setsid</tt> is called, a new <tt>session</tt> structure is allocated within the kernel. Now <tt>s_count</tt> is set to 1, <tt>s_leader</tt> is set to point to the <tt>proc</tt> structure of the calling process, <tt>s_sid</tt> is set to the process ID, and <tt>s_ttyvp</tt> and <tt>s_ttyp</tt> are set to null pointers, since the new session doesn't have a controlling terminal.</p>
<p class="docText">Let's move to the <tt>tty</tt> structure. The kernel contains one of these structures for each terminal device and each pseudo-terminal device. (We talk more about pseudo terminals in <a class="docLink" href="ch19.html#ch19">Chapter 19</a>.)</P>
<ul><LI><p class="docList"><tt>t_session</tt> points to the <tt>session</tt> structure that has this terminal as its controlling terminal. (Note that the <tt>tty</tt> structure points to the <tt>session</tt> structure and vice versa.) This pointer is used by the terminal to send a hang-up signal to the session leader if the terminal loses carrier (<a class="docLink" href="ch09lev1sec6.html#ch09fig07">Figure 9.7</a>).</p></LI><li><p class="docList"><tt>t_pgrp</tt> points to the <tt>pgrp</tt> structure of the foreground process group. This field is used by the terminal driver to send signals to the foreground process group. The three signals generated by entering special characters (interrupt, quit, and suspend) are sent to the foreground process group.</p></li><li><p class="docList"><tt>t_termios</tt> is a structure containing all the special characters and related information for this terminal, such as baud rate, is echo on or off, and so on. We'll return to this structure in <a class="docLink" href="ch18.html#ch18">Chapter 18</a>.</p></li><li><p class="docList"><tt>t_winsize</tt> is a <tt>winsize</tt> structure that contains the current size of the terminal window. When the size of the terminal window changes, the <tt>SIGWINCH</tt> signal is sent to the foreground process group. We show how to set and fetch the terminal's current window size in <a class="docLink" href="ch18lev1sec12.html#ch18lev1sec12">Section 18.12</a>.</p></li></ul>
<p class="docText">Note that to find the foreground process group of a particular session, the kernel has to start with the session structure, follow <tt>s_ttyp</tt> to get to the controlling terminal's <tt>tty</tt> structure, and then follow <tt>t_pgrp</tt> to get to the foreground process group's <tt>pgrp</tt> structure. The <tt>pgrp</tt> structure contains the information for a particular process group.</p>
<ul><li><p class="docList"><tt>pg_id</tt> is the process group ID.</p></li><li><p class="docList"><tt>pg_session</tt> points to the <tt>session</tt> structure for the session to which this process group belongs.</p></LI><LI><p class="docList"><tt>pg_members</tt> is a pointer to the list of <tt>proc</tt> structures that are members of this process group. The <tt>p_pglist</tt> structure in that <tt>proc</tt> structure is a <a name="idd1e66601"></a><a name="idd1e66606"></a><a name="idd1e66611"></a><a name="idd1e66616"></a><a name="idd1e66619"></a><a name="idd1e66624"></a><a name="idd1e66629"></a><a name="idd1e66632"></a>doubly-linked list entry that points to both the next process and the previous process in the group, and so on, until a null pointer is encountered in the <tt>proc</tt> structure of the last process in the group.</p></LI></UL>
<p class="docText">The <tt>proc</tt> structure contains all the information for a single process.</P>
<ul><LI><p class="docList"><tt>p_pid</tt> contains the process ID.</P></LI><li><p class="docList"><tt>p_pptr</tt> is a pointer to the <tt>proc</tt> structure of the parent process.</P></li><LI><p class="docList"><tt>p_pgrp</tt> points to the <tt>pgrp</tt> structure of the process group to which this process belongs.</P></LI><li><p class="docList"><tt>p_pglist</tt> is a structure containing pointers to the next and previous processes in the process group, as we mentioned earlier.</P></LI></ul>
<p class="docText">Finally, we have the <tt>vnode</tt> structure. This structure is allocated when the controlling terminal device is opened. All references to <tt>/dev/tty</tt> in a process go through this <tt>vnode</tt> structure. We show the actual i-node as being part of the v-node.</P>

<UL></ul></td></tr></table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch09lev1sec10.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch09lev1sec12.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html><br>
<table width="100%" cellspacing="0" cellpadding="0"
style="margin-top: 0pt; border-collapse: collapse;"> 
<tr> <td align="right" style="background-color=white; border-top: 1px solid gray;"> 
<a href="http://www.zipghost.com/" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">The CHM file was converted to HTM by Trial version of <b>ChmD<!--30-->ecompiler</b>.</a>
</TD>
</TR><tr>
<td align="right" style="background-color=white; "> 
<a href="http://www.etextwizard.com/download/cd/cdsetup.exe" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">Download <b>ChmDec<!--30-->ompiler</b> at: http://www.zipghost.com</a>
</TD></tr></table>
